package com.wectrls.common.utils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * @author chenjian
 * @Package
 * @Description: 时间处理工具类
 * @date 2019/7/17 15:42
 */

public class DateUtils {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 获取指定月份的第一毫秒
     *
     * @param DifferenceMonth 时间轴
     * @return
     */
    public static Long getMonthFirstMillis(Integer DifferenceMonth) {
        Calendar instance = Calendar.getInstance();
        int thisMonth = instance.get(Calendar.MONTH);
        instance.set(Calendar.MONTH, thisMonth + DifferenceMonth);
        instance.set(Calendar.DAY_OF_MONTH, 1);
        Instant instant = LocalDateTime.ofInstant(
                instance.toInstant(),
                ZoneId.systemDefault())
                .with(LocalTime.MIN)
                .atZone(ZoneId.systemDefault())
                .toInstant();
        return instant.toEpochMilli();
    }


    /**
     * 获取指定月份的最后一毫秒
     * <p>
     * 指定月份下一月第一毫秒减去一毫秒即为指定月份最后一毫秒
     *
     * @param DifferenceMonth
     * @return
     */
    public static Long getMonthLastMillis(Integer DifferenceMonth) {
        return getMonthFirstMillis(DifferenceMonth + 1) - 1L;
    }

    /**
     * 根据时间戳获取时间戳所在月的第一毫秒
     *
     * @param millis
     * @return
     */
    public static Long getMonthFirstMillis(Long millis) {
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(millis);
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH);
        instance.set(year, month, 1);
        return getDayOfBegin(instance.getTime());
    }

    /*public static void main(String[] args) {
        Long monthFirstMillis = getMonthFirstMillis(1568111587059L);
        System.out.println(monthFirstMillis);
        System.out.println(getFormatDate(monthFirstMillis));
        System.out.println("------");
        Long monthLastMillis = getMonthLastMillis(1568111587059L);
        System.out.println(monthLastMillis);
        System.out.println(getFormatDate(monthLastMillis));
    }*/

    /**
     * 根据时间戳获取时间戳所在月的最后一毫秒
     *
     * @param millis
     * @return
     */
    public static Long getMonthLastMillis(Long millis) {
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(millis);
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH);
        instance.set(year, month + 1, 1);
        Long dayOfBegin = getDayOfBegin(instance.getTime()) - 1L;
        return dayOfBegin;
    }

    /**
     * 获取指定日期第一毫秒
     *
     * @param date
     * @return
     */
    public static Long getDayOfBegin(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(LocalTime.MIN);
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return instant.toEpochMilli();
    }

    /**
     * 获取指定日期最后的时间
     *
     * @param date
     * @return
     */
    public static Long getDayOfEnd(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).with(LocalTime.MAX);
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return instant.toEpochMilli();
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static Date getDate() {
        return new Date();
    }

    /**
     * 获取当前格式化的时间
     *
     * @return
     */
    public synchronized static String getFormatDate() {
        return DATE_FORMAT.format(getDate());
    }

    /**
     * 根据毫秒值返回格式化的数据
     *
     * @param millis
     * @return
     */
    public synchronized static String getFormatDate(Long millis) {
        return DATE_FORMAT.format(new Date(millis));
    }

    /**
     * 获取格式化的时间
     *
     * @param date
     * @return
     */
    public synchronized static String getFormatDate(Date date) {
        return DATE_FORMAT.format(date);
    }

    /**
     * 获取当前时间戳
     *
     * @return
     */
    public static Long getTimeMillis() {
        return System.currentTimeMillis();
    }

    /**
     * 获取指定时间的毫秒值
     *
     * @param date
     * @return
     */
    public static Long getTimeMillis(Date date) {
        return date.toInstant().toEpochMilli();
    }

    /**
     * 获取指定几天前的最后一秒
     *
     * @param beforeDays
     * @return
     */
    public static Long getBeforeDayLastMillis(Integer beforeDays) {
        return getDayOfBegin(getDate()) - (beforeDays * 86400000L);
    }

    public static Integer getYear(Date date) {
        return getYear(date.getTime());
    }

    public static Integer getMonth(Date date) {
        return getMonth(date.getTime());
    }

    public static Integer getDayOfMonth(Date date) {
        return getDayOfMonth(date.getTime());
    }

    public static Integer getDayOfYear(Date date) {
        return getDayOfYear(date.getTime());
    }

    /**
     * 获取指定时间戳的年份（公元纪年）
     *
     * @param millis
     * @return
     */
    public static Integer getYear(Long millis) {
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(millis);
        int year = instance.get(Calendar.YEAR);
        return year;
    }

    public static Integer getMonth(Long millis) {
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(millis);
        int month = instance.get(Calendar.MONTH) + 1;
        return month;
    }

    public static Integer getDayOfMonth(Long millis) {

        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(millis);
        int dayOfMonth = instance.get(Calendar.DAY_OF_MONTH);
        return dayOfMonth;

    }

    public static Integer getDayOfYear(Long millis) {
        Calendar instance = Calendar.getInstance();
        instance.setTimeInMillis(millis);
        int dayOfYear = instance.get(Calendar.DAY_OF_YEAR);
        return dayOfYear;
    }


   /* public static void main(String[] args) {
        int dif = 0;
        Long monthFirstMillis = getMonthFirstMillis(dif);
        System.out.println(getFormatDate(monthFirstMillis));
        Long monthLastMillis = getMonthLastMillis(dif);
        System.out.println(getFormatDate(monthLastMillis));
        System.out.println(getFormatDate(getDayOfBegin(new Date())));
        System.out.println(getFormatDate(getDayOfEnd(new Date())));
        System.out.println(getTimeMillis(new Date()));
        Long beforDayLastMillis = getBeforeDayLastMillis(2);
        System.out.println(getFormatDate(beforDayLastMillis));

    }*/

}
